package com.pengtu.web.project;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.HibernateUtils;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.Organization;
import com.pengtu.entity.project.DrawingsContractInfo;
import com.pengtu.entity.project.SignUpContract;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Result(location = "/project/sign/construction.action?projectId=${projectId}&contractKind=${contractKind}", name = BaseActionSupport.RELOAD, type = "redirect")
public class ConstructionAction extends BaseActionSupport<SignUpContract> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProjectManager projManager;
	private AccountManager accountManager;

	private String id;
	private SignUpContract signUpContract;
	private Page<SignUpContract> page = new Page<SignUpContract>(10);
	private String projectId;
	private String contractKind;
	private String deptId;
	private List<DrawingsContractInfo> drawConList;
	private List<String> drawIdList;

	@Actions({
		@Action("/quality/org/construction"),
		@Action("/project/sign/construction")	
	})
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());

		queryfilter.addFilter("projectId", projectId);
		queryfilter.addFilter("contractKind" , contractKind);
		if (!page.isOrderBySetted()) {
			page.orderBy("createDate");
			page.order(Page.ASC);
		}
		page = projManager.findPageSign(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		drawConList = projManager.findDrawContractList(projectId);
		if (StringUtils.isNotEmpty(id))
			drawIdList = signUpContract.getDrawIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		HibernateUtils.mergeByCheckedIds(signUpContract.getContractSet(), drawIdList,
				DrawingsContractInfo.class);
		
		String deptId = Struts2Utils.getParameter("branchOrgId");
		String mark = Struts2Utils.getParameter("mark");
		String orgCode = Struts2Utils.getParameter("orgCode");
		//判断是否为直属合同段
		if("0".equals(mark)){
			Department dept = new Department();
			//获得单位信息
			Organization org = accountManager.getOrganization(orgCode);
			//将单位信息复制给部门表 
			if(null!=org){
				dept.setOrganization(org);
				dept.setDepCode(System.currentTimeMillis() + "");
				dept.setDepName(org.getOrgName());
				dept.setDepAllName(org.getOrgName());
				dept.setBurderMan(org.getPrincipal());
				dept.setRemark(org.getRemark());
			}
			signUpContract.setBranchOrgCode(dept);
		}
		if (StringUtils.isNotEmpty(deptId)) {
			Department dept = accountManager.getDepartment(deptId);
			if(null!=dept)
			signUpContract.setBranchOrgCode(dept);
		}
		projManager.saveOrupdateSign(signUpContract);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"施工单位合同" + signUpContract.getContractOrder());
		return "successMessage";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		/*
		 * addActionMessage(sign.getStartPegNum() + "至" + sign.getEndPegNum() +
		 * "的合同被删除");
		 */projManager.deleteSign(id);
		 addActionMessage("删除施工单位合同信息！");
		 Struts2Utils.renderText("1");
		return NONE;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			signUpContract = projManager.getSign("id", id);
		} else {
			signUpContract = new SignUpContract();
		}
	}
	
	/**
	 * 检查是否选择的为无效单位
	 * @throws UnsupportedEncodingException 
	 */
	public void checkOrgCode() throws UnsupportedEncodingException{
		String orgName = Struts2Utils.getParameter("orgName");
		Organization org = accountManager.getOrg("orgName",orgName);
		
		if(null!=org){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getContractKind() {
		return contractKind;
	}

	public void setContractKind(String contractKind) {
		this.contractKind = contractKind;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public Page<SignUpContract> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setProjManager(ProjectManager projManager) {
		this.projManager = projManager;
	}

	@Override
	public SignUpContract getModel() {
		// TODO Auto-generated method stub
		return signUpContract;
	}

	public List<String> getDrawIdList() {
		return drawIdList;
	}

	public void setDrawIdList(List<String> drawIdList) {
		this.drawIdList = drawIdList;
	}

	public List<DrawingsContractInfo> getDrawConList() {
		return drawConList;
	}

}
